package com.suse.util;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 拦截之前的操作
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("进入到了登录拦截器");
        // 获取用户是否登录
        Object obj = request.getSession().getAttribute("name");
        if(obj == null) {
            // 没有登录，跳转到登录页面
            request.getRequestDispatcher("/login").forward(request, response);
            return false;
        }
        return true;
    }

}
